﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PeonyShop.Bussiness;
using System.Data;
using PenoyShop.Utilities;
using System.Text;
using System.Web.Services;

namespace PeonyShop.Admin
{
    public partial class Category : System.Web.UI.Page
    {
        public string sbTable;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Admin"] != null)
            {
                if (!IsPostBack)
                {
                    BACategory bCate = new BACategory();
                    DataSet dsMain = bCate.GetAllCategoryByParenṭ(0, true, false);
                    
                    /* For add */
                    ddlCategory.DataTextField = "Name";
                    ddlCategory.DataValueField = "CateId";
                    ddlCategory.DataSource = dsMain.Tables[0];
                    ddlCategory.DataBind();
                    ddlCategory.Items.Insert(0, new ListItem("-- Please Choose --", "0"));

                    sbTable = ShowCate();

                }
            }
            else
                CodeHelper.Alert("Login trước rồi làm gì thì làm nhé", "Login.aspx");
        }

        [WebMethod]
        public static string ShowCate()
        {
            StringBuilder sTable = new StringBuilder();
            BACategory bCate = new BACategory();
            DataSet dsMain = bCate.GetAllCategoryByParenṭ(0, true, false);
            sTable.Append("<table id='tabCate' cellspacing='10'> <tr> <td colspan='2'> Category Name </td> <td> Order </td><td> Active </td><td> Delete </td><td></td> </tr> ");

            if (dsMain != null)
            {
                DataTable dtMain = dsMain.Tables[0];
                if (dtMain.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtMain.Rows)
                    {
                        DataSet dsSub = bCate.GetAllCategoryByParenṭ(Convert.ToInt32(dr["CateId"]), true, false);
                        sTable.Append("<tr>");
                        sTable.AppendFormat("<td><input type='text' value='{0}' /> </td>", dr["Name"].ToString());
                        sTable.Append("<td></td>");
                        sTable.AppendFormat("<td><input type='text' value='{0}' style='width:30px;'/> </td>", dr["OrderN"].ToString());
                        sTable.AppendFormat("<td><input type='checkbox' {0}'/> </td>", (dr["IsActive"].ToString() == "True") ? "checked = 'checked'" : "");
                        sTable.AppendFormat("<td><input type='checkbox' {0}'/> </td>", (dr["IsDelete"].ToString() == "True") ? "checked = 'checked'" : "");
                        sTable.AppendFormat("<td><input type='hidden' value='{0}' /> </td>", dr["CateId"].ToString());
                        sTable.Append("<td><img width='18px' heigth='18px'/></td>");
                        sTable.Append("</tr>");
                        if (dsSub != null)
                        {
                            DataTable dtSub = dsSub.Tables[0];
                            if (dtSub.Rows.Count > 0)
                            {
                                foreach (DataRow drSub in dtSub.Rows)
                                {
                                    sTable.Append("<tr>");
                                    sTable.Append("<td></td>");
                                    sTable.AppendFormat("<td><input type='text' value='{0}' /> </td>", drSub["Name"].ToString());
                                    sTable.AppendFormat("<td><input type='text' value='{0}' style='width:30px;'/> </td>", drSub["OrderN"].ToString());
                                    sTable.AppendFormat("<td><input type='checkbox' {0}'/> </td>", (dr["IsActive"].ToString() == "True") ? "checked = 'checked'" : "");
                                    sTable.AppendFormat("<td><input type='checkbox' {0}'/> </td>", (dr["IsDelete"].ToString() == "True") ? "checked = 'checked'" : "");
                                    sTable.AppendFormat("<td><input type='hidden' value='{0}' /> </td>", drSub["CateId"].ToString());
                                    sTable.Append("<td><img width='18px' heigth='18px'/></td>");
                                    sTable.Append("</tr>");
                                }
                            }
                        }
                    }
                }
            }
            sTable.Append("</table>");
            return sTable.ToString();
        }

        [WebMethod]
        public static string UpdateCate(int CateId, string Name, string Description, bool IsActive, bool IsDelete, int Order)
        {
            BACategory bCate = new BACategory();
            bool bResult = bCate.UpdateCate(CateId, Name, Description, Order, IsActive, IsDelete);
            return bResult.ToString();
        }

        [WebMethod]
        public static string InsertCate(string Name, string Description, int ParentId, bool IsActive, bool IsDelete, int Order)
        {
            BACategory bCate = new BACategory();
            bool bResult = bCate.InsertCate(Name, Description, ParentId,IsActive, IsDelete, Order);
            return bResult.ToString();
        }
    }
}